package com.bw.ivideo.model

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bw.ivideo.intent.LoginIntent
import com.bw.ivideo.state.LoginState
import com.bw.mvicore.model.BaseModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import java.util.UUID
import kotlin.random.Random
import kotlin.random.nextInt

/**
 * @author:created by hahajing
 * 时间：2024/4/22 0022
 * 企鹅号:444511958
 * 描述:
 */
class LoginModel:BaseModel<LoginIntent>() {

    override fun handleIntent() {
        viewModelScope.launch {
            _intents.consumeAsFlow().collect{
                when(it){
                    is LoginIntent.LoginRealIntent->{
                        login(it.username,it.pwd)
                    }
                }
            }
        }
    }

    private val _state = MutableStateFlow<LoginState>(LoginState.LoginDefault)
    val state:MutableStateFlow<LoginState>
        get() = _state

    suspend fun login(username:String,pwd:String){
        //模拟网络请求
        delay(500)
        _state.value=LoginState.LoginSuccess("用户：张三")
    }

}